package com.evil.admin.controller;

import com.evil.account.api.RemoteLoginService;
import com.evil.common.account.bo.AuthUserBO;
import com.evil.common.account.dto.LoginReqDTO;
import com.evil.common.account.dto.LogoutReqDTO;
import com.evil.common.account.enums.AppTypeEnum;
import com.evil.common.account.enums.PlatformTypeEnum;
import com.evil.common.account.util.LoginUtil;
import com.evil.common.core.entity.R;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录接口
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Slf4j
@RestController
@RequestMapping
@AllArgsConstructor
public class AdminLoginController {

    private final LoginUtil loginUtil;

    private final RemoteLoginService remoteLoginService;

    /**
     * 登录
     *
     * @param loginReqDTO loginReqDTO
     * @return R
     */
    @PostMapping("/login")
    public R<AuthUserBO> login(@Validated @RequestBody LoginReqDTO loginReqDTO) {
        loginReqDTO.setPlatformType(PlatformTypeEnum.ADMIN_PLATFORM.getId());
        return R.success(remoteLoginService.login(loginReqDTO));
    }

    /**
     * 登录出
     *
     * @return R
     */
    @PostMapping("/logout")
    public R<?> logout() {
        LogoutReqDTO logoutReqDTO = new LogoutReqDTO();
        logoutReqDTO.setAppType(AppTypeEnum.ADMIN.getId());
        logoutReqDTO.setToken(loginUtil.getLoginToken());
        remoteLoginService.logout(logoutReqDTO);
        return R.success();
    }
}
